package com.jhqc.pxsj.msa.sms.provider.controller;

import com.jhqc.pxsj.msa.sms.api.req.AddSendAllReq;
import com.jhqc.pxsj.msa.sms.api.req.AddSendReq;
import io.swagger.annotations.Api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jhqc.pxsj.msa.pub.common.ObjectResp;
import com.jhqc.pxsj.msa.pub.common.RespCode;
import com.jhqc.pxsj.msa.sms.api.SmsApi;
import com.jhqc.pxsj.msa.sms.api.dto.SmsDTO;
import com.jhqc.pxsj.msa.sms.api.dto.SmsTemplateDTO;
import com.jhqc.pxsj.msa.sms.api.url.SmsUrls;
import com.jhqc.pxsj.msa.sms.provider.service.SmsService;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import javax.validation.Valid;

/** 
 * sms短信微服务controller<p>
 * @author luochuan 
 * @date 2017年4月27日<br>
 * @version 1.0<br>
 */

@RequestMapping(SmsUrls.BASE_URL)
@Api(tags="(30401)短信相关接口")
@RestController
public class SmsController implements SmsApi{
	
	@Autowired
	SmsService smsService;

	private static Logger log = LoggerFactory.getLogger(SmsController.class);
	
	
	/**
     * 获取短信验证码
     * @param mobile
     * @param template_id
     * @param item
     * @return
     * @throws Exception
     */
	@RequestMapping(value = SmsUrls.GET_CAPTCHA, method = RequestMethod.GET)
	@Override
	public ObjectResp<SmsDTO> getCaptcha() {
		// TODO Auto-generated method stub
		return null;
	}

	
	/**
     * 发送短信到阿里大于
     * @param mobile
     * @param template_id
     * @param item
     * @return
     * @throws Exception
     */
	
	@RequestMapping(value = SmsUrls.SAVE_CAPTCHA, method = RequestMethod.GET)
	@Override
	public ObjectResp<SmsDTO> sendCaptcha(@RequestParam("mobile") String mobile, 
    		@RequestParam("appId") String appId,
    		@RequestParam(value = "templateId") String templateId,
    		@RequestParam(value = "item") String item){

		log.info("SEND SMS - " + mobile + " - " + templateId + " - " + item + " - " + appId );
		return smsService.sendSmsCaptcha(mobile ,templateId , item);
	}

	@Override
	public ObjectResp<SmsDTO> sendCaptcha(@Valid @RequestBody AddSendReq req) {
		log.info("SEND SMS - " + req.getAppId() + " - " + req.getTemplateId() + " - " + req.getItem() + " - " + req.getMobile() );

		if (null == req.getTemplateId()){
			return new ObjectResp<SmsDTO>(20000001 , "缺少参数模版ID");
		}
		if (null == req.getMobile()){
			return new ObjectResp<SmsDTO>(20000001 , "缺少参数手机号");
		}
		return smsService.sendCaptcha(req);
	}

	@Override
	public ObjectResp<SmsDTO> sendAllCaptcha(@Valid @RequestBody AddSendAllReq req) {


		log.info("SEND SMS - " + req.getAppId() + " - " + req.getTemplateId() + " - " + req.getItem() + " - " + req.getMobiles() );
		if (req.getMobiles() != null && req.getMobiles().size() >= 1){
			for (String mobile:req.getMobiles()) {
				try {
					AlibabaAliqinFcSmsNumSendResponse result = smsService.sendCaptcha(mobile,req.getTemplateId(),req.getItem());
					log.info(result.getBody());
					if (result.isSuccess()){
						return new ObjectResp<SmsDTO>(RespCode.SUCCESS);
					}else {
						return new ObjectResp<SmsDTO>(20000002 , "发送失败! 失败原因：" + result.getSubMsg());
					}
				} catch (Exception e) {
					return new ObjectResp<SmsDTO>(20000001 , "发送异常失败原因：" + e.getMessage());
				}
			}
		}
		return new ObjectResp<SmsDTO>(20000001 , "发送异常失败原因:手机号码不存在");


	}

	/**
     * 查询短信验证码
     * @param mobile
     * @param template_id
     * @param item
     * @return
     * @throws Exception
     */
	
	@RequestMapping(value = SmsUrls.FIND_TEMPLATE, method = RequestMethod.GET)
	@Override
	public ObjectResp<List<SmsTemplateDTO>> findTemplate() {
		List<SmsTemplateDTO> result = smsService.findTemplate();
		if (null == result) {
			return ObjectResp.valueOf(RespCode.RESOURCE_NOT_FOUND);
		}
		return new ObjectResp<List<SmsTemplateDTO>>(result);
	}

}
